package com.example.service;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.neo.JpaThymeleafApplication;
import com.neo.entity.Order;
import com.neo.repository.UserRepository;
import com.neo.util.ReadFromFile;



public class CleanDB {

    
    int sum=0;
    @Autowired
    private UserRepository userRepository;
    List<String> duList= new ArrayList<String>();
    @Test
    public void startClean() {
    	long start = System.currentTimeMillis();
    	List list = ReadFromFile.readFileByLines("D:/千百块10月数据带手机号.csv");
    	for (int i = 1; i < list.size(); i++) {//test
			String[] s = (String[])list.get(i);
			String loan_id = s[0];
			String mobile = s[1];
			String sfz = s[2];
			String account_id = s[3];
			int loanIds = userRepository.countUserIdByLoanId(loan_id);
    	    if (loanIds>=1){
    	    	if (loanIds>1){
    	    	   System.out.println("不可能出现");
    	    	}
    	    	System.out.println("loan_id已存在,不处理");
    	    }else{
    			excuteClean(mobile,loan_id,account_id);
    	    }
		}
    	long end = System.currentTimeMillis();
    	System.out.println("=========="+(end-start)/1000);
    	System.out.println("sum=========="+sum);
    	for (Object object : duList) {
			System.out.print("'"+object+"',");
		}
    }
    

    public void excuteClean(String mobile,String loan_id, String account_id) {
    	List<Object[]> list = userRepository.selectOrder(mobile);
    	for (Object orderid : list) {
    		logger.info("orderid========="+orderid);
        	int i = userRepository.updateOrder(loan_id,account_id,orderid+"");
        	if(i>=2){
        		System.out.println("");
        	}
        	System.out.println(i);
        	sum++;
		}
    }
    
//  public void excuteClean(String mobile,String loan_id,String account_id) {
////List list = userRepository.selectUser(sfz);
////if (list.size()>1){
////	for (Object object : list) {
////		String user_id = (String)object;
////		System.out.println(user_id);
////		duList.add(user_id);
////	}
////}
//List<Object[]> ifHad = userRepository.selectUserId(loan_id);
//if (ifHad.size()>1){
//	System.out.println("不可能出现");
//}
//if (ifHad.size()>=1){
//	System.out.println("loan_id已存在,不处理");
//}else{
//	excuteClean(mobile,loan_id,account_id);
//}
//
////for (Object object : list) {
////	String user_id = (String)object;
////   
////    if (ifHad.size()==1){
////    	  if (list.size()>1){
////    	    	System.out.println("");
////    	    }
////    	if (user_id.equals(ifHad.get(0)+"")){
////	    	logger.info("已有订单"+user_id);
////    	}else{
////    		getOrder(user_id,loan_id,account_id);
////    	}
////    }
////}
//}
    private final Logger logger = LoggerFactory.getLogger(this.getClass());  
   
    
    //=============================================
    public void all() {
    	for (int i = 0; i <10; i++) {
			if (i==5)
				return;
		}
    	System.out.println("1111");
    }
    public static void main(String[] args) {
		new CleanDB().findDouble();
	}
    
    public static List<Order> findDouble() {
    	List list = ReadFromFile.readFileByLines("D:/模板.csv");
    	List<Order> newList = new ArrayList<>();
    	for (int i = 1; i < list.size(); i++) {//test
			String[] s = (String[])list.get(i);
			Order o = new Order();
			o.setLoanid(s[0]);
			o.setUsername(s[2]);
	    	System.out.println(s[2]);
		}
       	return newList;
    }
}